from fastapi import APIRouter
import json
from pojo.pojo import *
from dbo.dbutil import *
from util.llm import *
app = APIRouter()

@app.post("/createDT")
async def createDT(data:DistributeTask,user:User):
    rs = {"code":200,'msg':"成功",'data':[]}
    # print(data)
    if data.id is None:
        sql = '''
        INSERT INTO distributetask(title,userGroupIdList,userId,`desc`,startTime,endTime,questionnaireIdList)
        VALUES('{}','{}','{}','{}','{}','{}','{}')
        '''.format(data.title,','.join(data.userGroupIdList),user.id,data.desc,data.startTime,data.endTime,','.join(data.questionnaireIdList))
    else:
        sql = '''
        UPDATE distributetask SET title='{}',userGroupIdList='{}',`desc`='{}',startTime='{}',endTime='{}',questionnaireIdList='{}'
        WHERE id = '{}'
        '''.format(data.title,','.join(data.userGroupIdList),data.desc,data.startTime,data.endTime,','.join(data.questionnaireIdList),data.id)
    print(sql)
    data = Mysql().insertDB(sql)
    if data==0:
        rs["code"]=500
        rs["msg"]="创建分发任务失败，程序员紧急处理中"
        return rs
    return rs


@app.post("/deleteDTByID")
async def deleteDTByID(data:DistributeTask,user:User):
    rs = {"code":200,'msg':"成功",'data':[]}
    sql = '''
    DELETE FROM distributetask
    WHERE id = '{}'
    '''.format(data.id)
    print(sql)
    data = Mysql().insertDB(sql)
    if data==0:
        rs["code"]=500
        rs["msg"]="删除分发任务失败，程序员紧急处理中"
        return rs
    return rs

@app.post("/getDTByID")
async def getDTByID(data:DistributeTask):
    rs = {"code":200,'msg':"成功",'data':[]}
    sql = '''
    SELECT * FROM distributetask
    JOIN user ON user.id =  distributetask.userId
    WHERE distributetask.id = '{}'
    '''.format(data.id)
    print(sql)
    data = Mysql().selectDB(sql,'one')
    
    
    sql = '''
    SELECT * FROM questionnaire 
    WHERE id IN ({})
    '''.format(data["questionnaireIdList"])
    print(sql)
    qnListData = Mysql().selectDB(sql,'many')
    data.update({"qnListData":qnListData})
    sql = '''
    SELECT * FROM questionnairerecord 
    JOIN user ON user.id=questionnairerecord.userId
    WHERE distributeTaskId='{}'
    '''.format(data["id"])
    print(sql)
    qnRecordListData = Mysql().selectDB(sql,'many')
    if data is None:
        rs["code"]=500
        rs["msg"]="查询失败，程序员修复中"
        return rs
    print(data)
    for r in qnRecordListData:
        r["record"] = json.loads(r["record"])
        r["report"] = json.loads(r["report"])
    data.update({"qnRecordListData":qnRecordListData})
    rs["data"]=data
    return rs

@app.post("/getDTListByUser")
async def getDTListByUser(user:User):
    rs = {"code":200,'msg':"成功",'data':[]}
    sql = '''
    SELECT * FROM distributetask 
    JOIN user ON user.id =  distributetask.userId
    WHERE distributetask.userId = '{}'
    '''.format(user.id)
    print(sql)
    data = Mysql().selectDB(sql,'many')
    if data is None:
        rs["code"]=500
        rs["msg"]="查询失败，程序员修复中"
        return rs
    for item in data:
        item["userGroupIdList"] = [] if item["userGroupIdList"]=='' or item["userGroupIdList"] is None else item["userGroupIdList"].split(",")
        item["questionnaireIdList"] = [] if item["questionnaireIdList"]=='' or item["questionnaireIdList"] is None else item["questionnaireIdList"].split(",")
    rs["data"]=data
    return rs


